<!doctype html>
<html lang="zh-Hans">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="hugo-theme" content="Axiom 0.7.1">



  <link rel="icon" type="image/png" sizes="32x32" href="/blog/">
  <link rel="icon" type="image/x-icon" href="/blog/">
  <link rel="apple-touch-icon" href="/blog/">
  <link rel="canonical" href="https://Mia-zhao.gitee.io/blog/vue-3-%E4%B9%8B-i18n/">
<link rel="preload" as="style" href="/blog/bundle.css?v=1603792187" media="all">
<link rel="stylesheet" href="/blog/bundle.css?v=1603792187" media="all">
<style>.cdata pre{color:#edf2f7;background-color:#2d3748}.cdata :not(pre)>code{color:#805ad5;background-color:#f7fafc}.chroma .err{color:#fed7d7;background-color:#9b2c2c}.chroma .hl{background-color:#4a5568}.chroma .ln{color:#a0aec0}.chroma .k,.chroma .kc,.chroma .kd,.chroma .kn,.chroma .kp,.chroma .kr{color:#63b3ed}.chroma .kt{color:#b794f4}.chroma .na{color:#f6e05e}.chroma .nb{color:#f6ad55}.chroma .nc{color:#fc8181}.chroma .no{color:#68d391}.chroma .nd{color:#fc8181}.chroma .ne{color:#fc8181}.chroma .nf{color:#f6ad55}.chroma .nt{color:#fc8181}.chroma .l{color:#b794f4}.chroma .dl,.chroma .ld,.chroma .s,.chroma .s2,.chroma .sa,.chroma .sb,.chroma .sc,.chroma .sd{color:#68d391}.chroma .se{color:#a0aec0}.chroma .s1,.chroma .sh,.chroma .si,.chroma .sr,.chroma .ss,.chroma .sx{color:#68d391}.chroma .il,.chroma .m,.chroma .mb,.chroma .mf,.chroma .mh,.chroma .mi,.chroma .mo{color:#b794f4}.chroma .o,.chroma .ow{color:#90cdf4}.chroma .p{color:#cbd5e0}.chroma .c,.chroma .c1,.chroma .ch,.chroma .cm,.chroma .cp,.chroma .cpf,.chroma .cs{color:#a0aec0}.chroma .ge{font-style:italic}.chroma .gs{font-weight:700}</style>



<title>Vue 3 之 i18n : Mia&#39;s Blog Posts</title>

<meta property="og:title" content="Vue 3 之 i18n">
<meta property="og:site_name" content="Mia&#39;s Blog Posts">
<meta property="og:url" content="https://Mia-zhao.gitee.io/blog/vue-3-%E4%B9%8B-i18n/">
<link rel="image_src" href="https://Mia-zhao.gitee.io/blog/">
<meta property="og:image" content="https://Mia-zhao.gitee.io/blog/">
<meta property="og:image:width" content="">
<meta property="og:image:height" content="">
<meta property="og:type" content="article">
<meta property="og:locale" content="zh_Hans">
<meta property="og:description" content="vue-i18n为 Vue 提供 i18n (internationalization) 插件支持，在 Vue 3 中使用 vue-i18n 对比 Vue 2 稍有不同。 安装 需要安装支持 Vue 3 的插件版本npm install vue-i18n@next或者yarn add vue-i18n@next。 使用 在main.js/main.ts同一路径下创建i18n.js/i18n.ts: import { createI18n } from &#39;vue-i18n&#39;const i18n = createI18n({locale: &#39;zh&#39;,messages: {en: {message: {hello: &#39;Hello&#39;}},zh: {message: {hello: &#39;你好&#39;}}}})export default i18n在main.js/main.ts中: import { createApp } from &#39;vue&#39;import App from &#39;.">
<meta name="description" content="vue-i18n为 Vue 提供 i18n (internationalization) 插件支持，在 Vue 3 中使用 vue-i18n 对比 Vue 2 稍有不同。 安装 需要安装支持 Vue 3 的插件版本npm install vue-i18n@next或者yarn add vue-i18n@next。 使用 在main.js/main.ts同一路径下创建i18n.js/i18n.ts: import { createI18n } from &#39;vue-i18n&#39;const i18n = createI18n({locale: &#39;zh&#39;,messages: {en: {message: {hello: &#39;Hello&#39;}},zh: {message: {hello: &#39;你好&#39;}}}})export default i18n在main.js/main.ts中: import { createApp } from &#39;vue&#39;import App from &#39;.">
<meta property="og:updated_time" content="2020-10-07T04:20:00Z">
<meta property="fb:app_id" content="">
<meta name="author" content="Mia Zhao">
<meta property="article:author" content="https://mia-zhao.gitee.io/">
<meta property="article:published_time" content="2020-10-07T04:20:00Z">
<meta property="article:modified_time" content="2020-10-07T04:20:00Z">
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Article",
  "headline": "Vue 3 之 i18n",
  "alternativeHeadline": "vue-i18n为 Vue 提供 i18n (internationalization) 插件支持，在 Vue 3 中使用 vue-i18n 对比 Vue 2 稍有不同。 安装 需要安装支持 Vue 3 的插件版本npm install vue-i18n@next或者yarn add vue-i18n@next。 使用 在main.js/main.ts同一路径下创建i18n.js/i18n.ts: import { createI18n } from 'vue-i18n'\rconst i18n = createI18n({\rlocale: 'zh',\rmessages: {\ren: {\rmessage: {\rhello: 'Hello'\r}\r},\rzh: {\rmessage: {\rhello: '你好'\r}\r}\r}\r})\rexport default i18n\r在main.js/main.ts中: import { createApp } from 'vue'\rimport App from '.",
  "url": "https://Mia-zhao.gitee.io/blog/vue-3-%E4%B9%8B-i18n/",
  "image": "https://Mia-zhao.gitee.io/blog/",
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "https://Mia-zhao.gitee.io/blog/vue-3-%E4%B9%8B-i18n/"
  },
  "description": "vue-i18n为 Vue 提供 i18n (internationalization) 插件支持，在 Vue 3 中使用 vue-i18n 对比 Vue 2 稍有不同。 安装 需要安装支持 Vue 3 的插件版本npm install vue-i18n@next或者yarn add vue-i18n@next。 使用 在main.js/main.ts同一路径下创建i18n.js/i18n.ts: import { createI18n } from 'vue-i18n'\rconst i18n = createI18n({\rlocale: 'zh',\rmessages: {\ren: {\rmessage: {\rhello: 'Hello'\r}\r},\rzh: {\rmessage: {\rhello: '你好'\r}\r}\r}\r})\rexport default i18n\r在main.js/main.ts中: import { createApp } from 'vue'\rimport App from '.",
  "author": {
    "@type": "Person",
    "name": "Mia Zhao"
  },
  "publisher": {
    "@type": "Organization",
    "name": "Mia's Blog Posts",
    "logo": {
      "@type": "ImageObject",
      "url": "https://Mia-zhao.gitee.io/blog/"
    }
  },
  "datePublished": "2020-10-07T04:20:00Z",
  "dateModified": "2020-10-07T04:20:00Z",
  "articleBody": "\u003cp\u003e\u003ccode\u003evue-i18n\u003c/code\u003e为 Vue 提供 i18n (internationalization) 插件支持，在 Vue 3 中使用 \u003ccode\u003evue-i18n\u003c/code\u003e 对比 Vue 2 稍有不同。\u003c/p\u003e\n\u003ch2 id=\"安装\"\u003e安装\u003c/h2\u003e\n\u003cp\u003e需要安装支持 Vue 3 的插件版本\u003ccode\u003enpm install vue-i18n@next\u003c/code\u003e或者\u003ccode\u003eyarn add vue-i18n@next\u003c/code\u003e。\u003c/p\u003e\n\u003ch2 id=\"使用\"\u003e使用\u003c/h2\u003e\n\u003cp\u003e在\u003ccode\u003emain.js\u003c/code\u003e/\u003ccode\u003emain.ts\u003c/code\u003e同一路径下创建\u003ccode\u003ei18n.js\u003c/code\u003e/\u003ccode\u003ei18n.ts\u003c/code\u003e:\u003c/p\u003e\n\u003cpre\u003e\u003ccode\u003eimport { createI18n } from 'vue-i18n'\r\n\r\nconst i18n = createI18n({\r\n  locale: 'zh',\r\n  messages: {\r\n    en: {\r\n      message: {\r\n        hello: 'Hello'\r\n      }\r\n    },\r\n    zh: {\r\n      message: {\r\n        hello: '你好'\r\n      }\r\n    }\r\n  }\r\n})\r\n\r\nexport default i18n\r\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e在\u003ccode\u003emain.js\u003c/code\u003e/\u003ccode\u003emain.ts\u003c/code\u003e中:\u003c/p\u003e\n\u003cpre\u003e\u003ccode\u003eimport { createApp } from 'vue'\r\nimport App from './App.vue'\r\nimport i18n from './i18n'\r\n\r\nconst app = createApp(App)\r\napp.use(i18n)\r\napp.mount('#app')\r\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e这样为了实现 i18n，可以在组件中用\u003c/p\u003e\n\u003cpre\u003e\u003ccode\u003e\u0026lt;i18n-t tag=\u0026quot;h2\u0026quot; keypath=\u0026quot;message.hello\u0026quot; /\u0026gt;\r\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003e或者\u003c/p\u003e\n\u003cpre\u003e\u003ccode\u003e\u0026lt;h2\u0026gt;{{ $t('message.hello') }}\u0026lt;/h2\u0026gt;\r\n\u003c/code\u003e\u003c/pre\u003e"
}
</script>

<link rel="preload" as="script" href="/blog/bundle.js?v=1603792187">


</head>
<body>

  <header id="nav" class="header">
  <div class="ax-l-i max-w-6xl">
    <div class="ax-logo">
      <a class="block" href="/blog/" title="Mia&#39;s Blog Posts"><span class="font-semibold text-raven-900">Mia's Blog Posts</span></a>
    </div>
    <div class="ax-user">
      <a class="p-2 w-8 h-8 block text-raven-500 hover:text-gray-800 focus:text-gray-800 focus:outline-none" target="_blank" rel="noopener nofollow" href="https://www.google.com/search?q=site:https://Mia-zhao.gitee.io/blog/" title="Search">
        <svg class="fill-current" viewBox="0 0 32 32" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M2.67 12.804c0-5.6 4.544-10.134 10.133-10.134s10.134 4.544 10.134 10.134-4.544 10.133-10.134 10.133S2.67 18.393 2.67 12.804zm28.943 16.923l-8.868-8.868c4.287-5.3 3.68-13.012-1.378-17.57S8.564-1.066 3.75 3.75s-5.017 12.558-.46 17.618 12.28 5.665 17.57 1.378l8.868 8.868a1.33 1.33 0 0 0 2.231-.597c.123-.46-.008-.952-.345-1.29h0z"/></svg>

      </a>
      <a class="p-2 block text-base leading-none text-raven-500 hover:text-gray-800 focus:text-gray-800 focus:outline-none" href="/blog/posts/">
        Posts
      </a>
    </div>
  </div>

  
</header>

  <main>
<div class="default-single">
  <div class="ax-title ax-l-o">
    <div class="ax-l-i max-w-680">
      <h1 class="post-title font-content-title font-normal leading-tight tracking-default text-40">Vue 3 之 i18n</h1>

      <div class="ax-meta flex items-center mt-5">
        <div class="flex-grow min-w-0">
          <div class="flex items-center">
  <div class="flex-shrink-0">
    <img
    class="w-12 h-12 sm:w-14 sm:h-14 object-cover p-3px rounded-full border border-blue-300"
    src="/blog/images/author/default.webp"
    alt="Mia Zhao">
  </div>
  <div class="flex-shrink-0 ml-2 leading-tight font-content-sans">
    <a class="block text-sm text-raven-800 hover:text-raven-900 hover:underline focus:underline" target="_blank" rel="noopener nofollow" title="Mia Zhao" href="https://mia-zhao.gitee.io/">Mia Zhao</a>
    <time class="text-sm text-raven-500" datetime="2020-10-07T04:20:00Z">Oct 7, 2020 12:20PM</time>
  </div>
</div>

        </div>
        <div>
          <div class="flex items-center">
  <a class="flex-shrink-0 block text-raven-800 hover:text-raven-900" target="_blank" rel="noopener nofollow" title="Share on Twitter" href="https://twitter.com/intent/tweet?text=Vue%203%20%e4%b9%8b%20i18n%20by%20%40%25%21s%28%3cnil%3e%29%20https%3a%2f%2fMia-zhao.gitee.io%2fblog%2fvue-3-%25E4%25B9%258B-i18n%2f"><svg class="w-6 h-6 fill-current" viewBox="0 0 32 32" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M32 6.078c-1.2.522-2.458.868-3.78 1.036 1.36-.812 2.398-2.088 2.886-3.626a13.11 13.11 0 0 1-4.16 1.588C25.742 3.794 24.026 3 22.154 3a6.56 6.56 0 0 0-6.556 6.562c0 .52.044 1.02.152 1.496-5.454-.266-10.28-2.88-13.522-6.862-.566.982-.898 2.106-.898 3.316a6.57 6.57 0 0 0 2.914 5.452 6.48 6.48 0 0 1-2.964-.808v.072c0 3.188 2.274 5.836 5.256 6.446-.534.146-1.116.216-1.72.216-.42 0-.844-.024-1.242-.112.85 2.598 3.262 4.508 6.13 4.57a13.18 13.18 0 0 1-8.134 2.798c-.538 0-1.054-.024-1.57-.1C2.906 27.93 6.35 29 10.064 29c12.072 0 18.672-10 18.672-18.668 0-.3-.01-.57-.024-.848C30.014 8.56 31.108 7.406 32 6.078z"/></svg>
</a>
  <a class="ml-3 flex-shrink-0 block text-raven-800 hover:text-raven-900" target="_blank" rel="noopener nofollow" title="Share on Facebook" href="https://www.facebook.com/dialog/share?app_id=&display=page&href=https%3a%2f%2fMia-zhao.gitee.io%2fblog%2fvue-3-%25E4%25B9%258B-i18n%2f"><svg class="w-6 h-6 fill-current" viewBox="-7 -3.5 39 39" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M30.234 0H1.765C.8.001 0 .79 0 1.766v28.47C.001 31.2.79 32 1.766 32h15.328V19.625h-4.156V14.78h4.156v-3.564c0-4.134 2.523-6.384 6.21-6.384 1.766 0 3.284.13 3.726.2v4.32h-2.543c-2.006 0-2.394.953-2.394 2.352v3.085h4.797l-.625 4.844h-4.172V32h8.14C31.21 32 32 31.2 32 30.234V1.765C32 .8 31.21 0 30.234 0z"/></svg>
</a>
</div>

        </div>
      </div>
    </div>
  </div><div class="ax-content ax-l-o">
    <div class="ax-l-i max-w-680">
      <article class="cdata">
<p><code>vue-i18n</code>为 Vue 提供 i18n (internationalization) 插件支持，在 Vue 3 中使用 <code>vue-i18n</code> 对比 Vue 2 稍有不同。</p>
<h2 id="安装">安装</h2>
<p>需要安装支持 Vue 3 的插件版本<code>npm install vue-i18n@next</code>或者<code>yarn add vue-i18n@next</code>。</p>
<h2 id="使用">使用</h2>
<p>在<code>main.js</code>/<code>main.ts</code>同一路径下创建<code>i18n.js</code>/<code>i18n.ts</code>:</p>
<pre><code>import { createI18n } from 'vue-i18n'

const i18n = createI18n({
  locale: 'zh',
  messages: {
    en: {
      message: {
        hello: 'Hello'
      }
    },
    zh: {
      message: {
        hello: '你好'
      }
    }
  }
})

export default i18n
</code></pre><p>在<code>main.js</code>/<code>main.ts</code>中:</p>
<pre><code>import { createApp } from 'vue'
import App from './App.vue'
import i18n from './i18n'

const app = createApp(App)
app.use(i18n)
app.mount('#app')
</code></pre><p>这样为了实现 i18n，可以在组件中用</p>
<pre><code>&lt;i18n-t tag=&quot;h2&quot; keypath=&quot;message.hello&quot; /&gt;
</code></pre><p>或者</p>
<pre><code>&lt;h2&gt;{{ $t('message.hello') }}&lt;/h2&gt;
</code></pre>
      </article>
      

      

    </div>
  </div>
</div>

  </main>
  <footer class="footer">
  <div class="ax-l-i max-w-6xl">
    <nav class="flex items-center justify-center">
      <a class="ml-3 first:ml-0 text-sm text-gray-600 hover:text-gray-800" href="/blog/posts/">Posts</a>
      <a class="ml-3 first:ml-0 text-sm text-gray-600 hover:text-gray-800" href="/blog/contact/">Contact</a>
    </nav>

    <div class="footer-copyright text-sm text-center text-gray-500 mt-4">
      &#169; -2020 Mia&#39;s Blog Posts
    </div>
    <div class="text-sm sm:text-xs text-center text-gray-500 mt-2">
      Powered by <a href="https://www.axiomtheme.com/?utm_source=theme-footer&utm_medium=website&utm_campaign=referral">Axiom</a>
    </div>
  </div>
</footer>

<script src="/blog/bundle.js?v=1603792187"></script>


</body>
</html>
